<?php
declare(strict_types=1);

namespace App\Console\Commands\Tasks;


use App\Enums\StreamerEnum;
use App\Exceptions\BasicException;
use App\Models\MainDB\UserRoom;
use App\Repositories\Activity\MidAutumn2022\MidAutumn2022TaskRepository;
use App\Repositories\RoomPk\RoomPKPitRepository;
use App\Repositories\Streamer\StreamerRewardRepository;
use App\Repositories\Streamer\StreamerStatRepository;
use App\Repositories\Streamer\StreamerTaskRecordRepository;
use App\Repositories\Streamer\StreamerTaskRewardRepository;
use App\Resource\Streamer\Task\Resource;
use App\Services\Business\BusinessConfigService;
use App\Services\Tools\CurlService;
use App\Services\Tools\WechatAppletService;
use App\Services\Tools\YlService;
use App\Services\Tools\ZeGoService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class FixRoomPKCommand extends Command
{
    //命令名
    protected $signature = 'script:fixRoomPitPk {arg?}';

    //命令描述:上周周星数据入库，并且发送周星奖励
    protected $description = 'script:fixRoomPitPkByRoomCode';

    public function __construct()
    {
        parent::__construct();
    }

    //业务处理
    public function handle()
    {
        $roomCode = $this->argument("arg");
        if(!$roomCode){
            return $this->errorTips("请输入房间code");
        }

        $room = UserRoom::query()->where("room_id", $roomCode)->select(["id", "pit_pk_id", "room_name"])->first();
        if(!$room->id){
            return $this->errorTips("房间不存在");
        }
        if (!$room->pit_pk_id){
            return $this->errorTips("房间【{$room->room_name}($roomCode)】未进行麦位pk");
        }

        try{
            RoomPKPitRepository::getInstance()->finish($room->pit_pk_id);
        }catch (\Exception $exception){
            return $this->errorTips($exception->getMessage());
        }

        return $this->successTips("修复房间【{$room->room_name}($roomCode)】麦位pk成功: {$room->pit_pk_id}");
    }


    public function infoTips(string $message)
    {
        echo "\033[33m [INFO]\033[0m    {$message}" . PHP_EOL;
        return true;
    }

    public function errorTips(string $message)
    {
        echo "\033[31m [ERROR]\033[0m   {$message}" . PHP_EOL;
        return false;
    }

    public function successTips(string $message)
    {
        echo "\033[32m [SUCCESS]\033[0m {$message}" . PHP_EOL;
        return true;
    }
}